<?php

namespace app\admin\model\pifa;

use think\Model;
use think\Db;
use think\Exception;
use fast\Tongbu;
class Order extends Model
{

    

    

    // 表名
    protected $name = 'pifa_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'expiretime_text',
        'refundtime_text',
        'canceltime_text',
        'orderstate_text',
        'fenstate_text',
        'shippingstate_text',
        'paystate_text'
    ];
    
     protected static function init()
    {
        


         self::afterWrite(function ($row) {
            $changedData = $row->getChangedData();
            if (isset($changedData['orderstate'])) {
                if ($changedData['orderstate'] == 3) {
                    
                    if ($row['fenstate'] == 0) {
                        if (!empty($row['inv_id'])) {
                           
                            \think\Db::table("fa_pifa_order")->where('id', $row['id'])->update(['fenstate' => 1]);
                            \app\common\model\User::money($row['fen_price'], $row['inv_id'], $row['order_no'].'订单分佣');
                        }
                       

                    }
                    // if (empty($row['three_no'])) {
                    //     try {
                    //         $Tongbu = new Tongbu();
                    //         $result = $Tongbu->createOrder($row['id']);
                    //         if ($result && isset($result['Success']) && $result['Success'] == true) {
                    //             \think\Db::table("fa_pifa_order")->where('id', $row['id'])->update(['three_no' => $result['data']]);
                    //         } else {
                    //             Exception('同步失败');
                    //         }
                    //     } catch (\Exception $e) {
                    //         throw new Exception($e->getMessage());
                    //     }
                    // }
                   
                } 

              
            }
        });


    }
    
    public function getOrderstateList()
    {
        return ['0' => __('Orderstate 0'), '1' => __('Orderstate 1'), '2' => __('Orderstate 2'), '3' => __('Orderstate 3'), '4' => __('Orderstate 4')];
    }

    public function getFenstateList()
    {
        return ['0' => __('Fenstate 0'), '1' => __('Fenstate 1'), '2' => __('Fenstate 2')];
    }

    public function getShippingstateList()
    {
        return ['0' => __('Shippingstate 0'), '1' => __('Shippingstate 1'), '2' => __('Shippingstate 2')];
    }

    public function getPaystateList()
    {
        return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
    }

    public function getTuikuanList()
    {
        return ['0' => __('待申请'), '1' => __('待处理'), '2' => __('同意'), '3' => __('拒绝'),];
    }



    public function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiretime']) ? $data['expiretime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRefundtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refundtime']) ? $data['refundtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCanceltimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['canceltime']) ? $data['canceltime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderstateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['orderstate']) ? $data['orderstate'] : '');
        $list = $this->getOrderstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFenstateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['fenstate']) ? $data['fenstate'] : '');
        $list = $this->getFenstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShippingstateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shippingstate']) ? $data['shippingstate'] : '');
        $list = $this->getShippingstateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaystateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paystate']) ? $data['paystate'] : '');
        $list = $this->getPaystateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setExpiretimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


     public function item()
    {
        return $this->hasMany('Orderitem', 'order_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function inv()
    {
        return $this->belongsTo('app\admin\model\User', 'inv_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function address()
    {
        return $this->belongsTo('Useraddress', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function address2()
    {
        return $this->belongsTo('Useraddress', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function mendian()
    {
        return $this->belongsTo('Shangjia', 'mendian_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function peisong()
    {
        return $this->belongsTo('app\admin\model\User', 'peisong_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
